/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: cbus_pipe.c
 *
 * Purpose: abstract a communication pipe
 *
 * Developer:
 *   wen.gu , 2022-03-12
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "cbus/bus/cbus_pipe.h"


#include "cbus/core/cbus_log.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define CHECK_OPS_API(ep_ops, ops_api) if(!(ep_ops)->ops_api){ \
    LOGE("syntax error: pipe ops must implementation API:'%s'\n", #ops_api ); \
    return GFALSE; \
}
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/
struct _cbus_pipe
{
    GPHD pipe_handle;
    cbus_pipe_ops ops;
};
/******************************************************************************
 **    inner FUNCTION DEFINITIONS
 ******************************************************************************/
static GBOL cbus_pipe_ops_check_valid(cbus_pipe_ops* ops)
{    
    CHECK_OPS_API(ops, finish);
    CHECK_OPS_API(ops, send);
    CHECK_OPS_API(ops, recv);
    return GTRUE;
}
/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/

GErrc cbus_pipe_create(cbus_pipe_t* pipe, GPHD pipe_handle, const cbus_pipe_ops* ops)
{
    if (!pipe || !pipe_handle || !ops)
    {
        return G_ErrBadParameter;
    }

    if (cbus_pipe_ops_check_valid(ops) == GFALSE)
    {
        return G_ErrUndefined;
    }

    cbus_pipe_t cp = (cbus_pipe_t)malloc(sizeof(struct _cbus_pipe));

    if (!cp)
    {
        return G_ErrInsufficientResources;
    }

    /** Make a copy of the pipe operations.  This allows us to
	 *  modify them (to override NULLs for example), and avoids an extra
	 *  dereference on hot paths.
     */
    cp->ops = *ops;
    cp->pipe_handle = pipe_handle;

    *pipe = cp;

    return G_OK;
}

void cbus_pipe_destroy(cbus_pipe_t pipe)
{
    if (pipe)
    {
        GErrc ret = pipe->ops.finish(pipe->pipe_handle);

        if (G_OK != ret)
        {
            LOGE("finish pipe ops failed(0x%08x)\n", ret);
        }

        free(pipe);
    }
}

GErrc cbus_pipe_write(cbus_pipe_t pipe, const GBUF data, GU32 size)
{
    if (!pipe || !data || (size == 0))
    {
        return G_ErrBadParameter;
    }
    return pipe->ops.send(pipe->pipe_handle, data, size);
}

GS32 cbus_pipe_read(cbus_pipe_t pipe, GBUF buf, GU32 buf_size, GU32 timeout_ms)
{
    if (!pipe || !buf || (buf_size == 0))
    {
        return (GS32)G_ErrBadParameter;
    }

    return pipe->ops.recv(pipe->pipe_handle, buf, buf_size, timeout_ms);
}